package com.dq.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dq.commonutils.vo.Result;
import com.dq.eduservice.entity.EduCourse;
import com.dq.eduservice.entity.EduTeacher;
import com.dq.eduservice.service.EduCourseService;
import com.dq.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @project: guli_parent
 * @ClassName: FrontIndexController
 * @author: dq
 * @creat: 2022/8/5 16:18
 */
@RestController
@RequestMapping("/eduservice/index")
@CrossOrigin
public class FrontIndexController {
    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduTeacherService teacherService;

    /**
     * 1. 查询前8条课程
     * 2. 查询前4条老师
     * @return
     */
    @GetMapping
    public Result getIndex(){
        LambdaQueryWrapper<EduCourse> eduCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduCourseLambdaQueryWrapper.orderByDesc(EduCourse::getViewCount).last("limit 8");
        List<EduCourse> eduCourseList = courseService.list(eduCourseLambdaQueryWrapper);

        LambdaQueryWrapper<EduTeacher> eduTeacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduTeacherLambdaQueryWrapper.orderByDesc(EduTeacher::getLevel).last("limit 4");
        List<EduTeacher> eduTeachers = teacherService.list(eduTeacherLambdaQueryWrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("indexOfCourse",eduCourseList);
        map.put("indexOfTeacher",eduTeachers);

        return Result.success(map);
    }
}
